package com.softeem.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@Controller
public class CustomController {

    @RequestMapping("/file")
    @ResponseBody
    public String getfile(@Param("path") String path, @Param("resp") HttpServletResponse resp) throws IOException {
        //设置编码
        resp.setCharacterEncoding("utf-8");
        //设置下载信息
        resp.setHeader("Content-Disposition", "attachment; filename=" + path);
        //得到流
        FileInputStream fis = null;
        ServletOutputStream os = null;
        try {
            os = resp.getOutputStream();
            fis = new FileInputStream("D:\\SecondProject\\resource\\" + path);
            byte[] buf = new byte[1024];
            int len = 0;
            while((len = fis.read(buf)) > 0){
                os.write(buf,0,len);
            }
        } finally {
            if (os != null){
                os.close();
            }
            if(fis != null){
                fis.close();
            }
        }
        return null;
    }

    @RequestMapping("/stafflogout")
    public String stafflogout(@Param("req")HttpServletRequest req){
        req.getSession().removeAttribute("staffInfo");
        return "redirect:/staffPage/home.handler";
    }

    @RequestMapping("/Userlogout")
    public String Userlogout(@Param("req")HttpServletRequest req){
        req.getSession().removeAttribute("userInfo");
        return "redirect:/userPage/home.handler";
    }
}
